﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DualControl.Controllers;
using DualControl.Posts;
using DualControl.Posts.Dtos;
using Microsoft.AspNetCore.Mvc;
using X.PagedList;

namespace DualControl.Web.Mvc.Controllers
{
    public class PostsController : DualControlControllerBase
    {
        private readonly IPostAppService  _postAppService;

        public PostsController(IPostAppService postAppService)
        {
            _postAppService = postAppService;
        }

        //public async Task<ActionResult> Index(GetPostsInput input)
        public async Task<ActionResult> Index(int page)
        {
            int pagenumber = page < 1 ? 1 : page;
            GetPostsInput input = new  GetPostsInput
            {
                SkipCount = (pagenumber - 1) * AppConsts.DefaultPageSize,
                MaxResultCount = AppConsts.DefaultPageSize
            };
            var result = await _postAppService.GetPagedPosts(input);
            var PageOfPostListDto = new StaticPagedList<PostListDto>(result.Items, pagenumber, AppConsts.DefaultPageSize, result.TotalCount);
            //将分页结果放入ViewBag供View使用
            ViewBag.PageOfPostListDto = PageOfPostListDto;

            return View();
        }
    }
}